import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;

public static Result<String> formatAllDev(Map<String, String> cookiesMap, String formatProcessUrl, List<String> devs) {
    Result<String> result = new Result<>();
    String resutlCount = null;
    String state = null;
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpGet = new HttpGet(formatProcessUrl);
        httpGet.setHeader("User-Agent", USER_AGENT);

        // 设置请求配置
        RequestConfig requestConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY)
                .build();
        httpGet.setConfig(requestConfig);

        // 创建 HttpClientContext 并设置 Cookies
        HttpClientContext context = HttpClientContext.create();
        for (Map.Entry<String, String> entry : cookiesMap.entrySet()) {
            BasicClientCookie cookie = new BasicClientCookie(entry.getKey(), entry.getValue());
            context.getCookieStore().addCookie(cookie);
        }

        // 发送请求
        HttpResponse response = httpClient.execute(httpGet, context);
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String sysmData = EntityUtils.toString(response.getEntity(), "UTF-8");
                JSONObject parse = JSONObject.parseObject(sysmData);
                Integer status = parse.getInteger("errorcode");
                System.out.println("格式化中===》》" + parse.toJSONString());
                if (status == 200) {
                    JSONObject jsonData = (JSONObject) parse.get("data");
                    System.out.println(jsonData);
                    resutlCount = jsonData.getString("result");
                    state = jsonData.getString("state");
                } else {
                    result.setMsg("errorcode:" + status);
                    result.setCode(0);
                    return result;
                }
            }
        } finally {
            EntityUtils.consume(response.getEntity());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("格式化结果判定state= resutlCount===》》" + state + "=" + resutlCount);
    if (state.equals("0") && resutlCount.equals(devs.size() + "")) {
        result.setCode(1);
    } else {
        result.setCode(0);
    }
    return result;
}
